package src.com.neusoft.nep.controller;

import src.com.neusoft.nep.entity.DialogManager;
import src.com.neusoft.nep.common.FileUtils;
import src.com.neusoft.nep.entity.User;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Map;

public class PublicSupervisorRegisterViewController {

    @FXML
    private TextField txt_phoneNumber; // 手机号输入框
    @FXML
    private TextField txt_feedbackerName; // 真实姓名输入框
    @FXML
    private ComboBox<String> cmb_year; // 年下拉框
    @FXML
    private ComboBox<String> cmb_month; // 月下拉框
    @FXML
    private ComboBox<String> cmb_day; // 日下拉框
    @FXML
    private ToggleButton btn_male; // 男单选按钮
    @FXML
    private ToggleButton btn_female; // 女单选按钮
    @FXML
    private TextField txt_password; // 密码输入框
    @FXML
    private TextField txt_repassword; // 确认密码输入框
    @FXML
    private Button btn_back; // 返回按钮

    private String selectedRole; // 当前选中的角色
    private Stage currentStage;

    public void setLoginViewStage(Stage stage) {
        this.currentStage = stage;
    }

    // 初始化方法，用于设置界面初始化状态
    @FXML
    public void initialize() {
        // 初始化年月日下拉框数据
        for (int i = 1900; i <= 2025; i++) {
            cmb_year.getItems().add(String.valueOf(i));
        }
        for (int i = 1; i <= 12; i++) {
            cmb_month.getItems().add(String.valueOf(i));
        }
        for (int i = 1; i <= 31; i++) {
            cmb_day.getItems().add(String.valueOf(i));
        }

        // 默认选择当前年月日
        cmb_year.setValue(String.valueOf(2025));
        cmb_month.setValue(String.valueOf(6));
        cmb_day.setValue(String.valueOf(18));
    }

    @FXML
    public void saveUser() {
        try {
            String phoneNumber = txt_phoneNumber.getText().trim(); // 手机号
            String feedbackerName = txt_feedbackerName.getText().trim(); // 真实姓名
            String year = cmb_year.getValue(); // 年
            String month = cmb_month.getValue(); // 月
            String day = cmb_day.getValue(); // 日
            String gender = btn_male.isSelected() ? "男" : "女"; // 性别
            String password = txt_password.getText().trim(); // 密码
            String repassword = txt_repassword.getText().trim(); // 确认密码

            // 输入验证
            if (phoneNumber.isEmpty() || feedbackerName.isEmpty() || year.isEmpty() || month.isEmpty() || day.isEmpty() || password.isEmpty() || repassword.isEmpty()) {
                DialogManager.showDialog("用户注册", "请填写所有必填字段");
                return;
            }

            if (!password.equals(repassword)) {
                DialogManager.showDialog("用户注册", "两次输入的密码不一致");
                return;
            }

            User user = new User(phoneNumber, password, feedbackerName, year + "-" + month + "-" + day, gender);

            Map<String, User> userMap = FileUtils.readMapObject("Feedbacker.txt");

            if (userMap.containsKey(phoneNumber)) {
                DialogManager.showDialog("用户注册", "该账号已存在");
                return;
            }

            userMap.put(user.getPhoneNumber(), user);
            FileUtils.writeMapObject("Feedbacker.txt", userMap);

            DialogManager.showDialog("用户注册", "注册成功");

            // 注册成功后自动返回登录界面
            back();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    // 处理返回按钮点击事件的方法
    @FXML
    public void back() {
        // 获取当前舞台
        Stage currentStage = (Stage) btn_back.getScene().getWindow();
        // 关闭当前舞台
        currentStage.close();
    }
}